package org.fiebug.spring.boot.seata.account.controller;

import org.fiebug.spring.boot.seata.account.service.AccountService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping(value = {"/account"})
public class AccountController {
    @Resource
    private AccountService accountService;

    /**
     * 出账
     */
    @RequestMapping(value = {"/credit"}, method = {RequestMethod.GET})
    public ResponseEntity<String> credit(@RequestParam("id") Integer id, @RequestParam("amount") BigDecimal amount) {
        accountService.credit(id, amount);
        return ResponseEntity.ok("CREDIT SUCCESS");
    }

    /**
     * 入账
     */
    @RequestMapping(value = {"/debit"}, method = {RequestMethod.GET})
    public ResponseEntity<String> debit(@RequestParam("id") Integer id, @RequestParam("amount") BigDecimal amount) {
        accountService.debit(id, amount);
        return ResponseEntity.ok("DEBIT SUCCESS");
    }

    /**
     * 转账
     */
    @RequestMapping(value = {"/transfer"}, method = {RequestMethod.GET})
    public ResponseEntity<String> transfer(@RequestParam("creditId") Integer creditId, @RequestParam("debitId") Integer debitId, @RequestParam("amount") BigDecimal amount) {
        accountService.transfer(creditId, debitId, amount);
        return ResponseEntity.ok("TRANSFER SUCCESS");
    }
}
